package Homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/*在你生成的文件中，点名，奇数次点名的名字肯定是
张三-男-23*/
public class work08 {
    public static void main(String[] args) throws Exception {
/*		BufferedReader br = new BufferedReader(new FileReader("names.txt"));
		ArrayList<String> list = new ArrayList<>();
		String str = null;
		while ((str = br.readLine()) != null) {
			list.add(str);
		}
		br.close();

		System.out.println("请输入点名同学个数：");
		Scanner sc = new Scanner(System.in);
		Integer strCount = sc.nextInt();
		randomCallName(list,strCount);*/

        randomCallName1();
    }

    public static void randomCallName(ArrayList<String> list,int count){
        int temp = 0;
        for (int i = 1; i <= count; i++) {
            temp++;
            String calledName = "";

            if (temp % 2 != 0) { // 奇数次点名
                calledName = "张三-男-23";
            } else {
                Collections.shuffle(list);
                calledName = list.get(0);
            }

            System.out.println("第" + i + "次运行程序：随机同学姓名" + i + ": " + calledName);
        }
    }

    public static void randomCallName1() throws Exception{
        BufferedReader br = new BufferedReader(new FileReader("names.txt"));
        ArrayList<String> list = new ArrayList<>();
        String str = null;
        while ((str = br.readLine()) != null) {
            list.add(str);
        }
        br.close();

        File countFile=new File("count.txt");
        int count;
        if(countFile.exists()){
            BufferedReader br1 = new BufferedReader(new FileReader("count.txt"));
            count = Integer.parseInt(br1.readLine());
            br1.close();
        }else{
            count = 0;
            countFile.createNewFile();
        }

        if (count % 2 != 0) { // 奇数次点名
            System.out.println("张三-男-23");
            count++;
        } else {
            Collections.shuffle(list);
            System.out.println(list.get(0));
            count++;
        }
        BufferedWriter br2 = new BufferedWriter(new FileWriter("count.txt"));
        br2.write(String.valueOf(count));
        br2.close();
    }
}
